#include "myclockdemo.h"
#include <QTimer>
MyClockDemo::MyClockDemo(QWidget *parent) :
    QWidget(parent)
{
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    timer->start(1000);
    setWindowTitle("MyClock Demo");
    resize(300,300);
    mIsSelected=false;
}
#include <QPainter>
#include <QDateTime>
#include <QPaintEvent>
#include <QDebug>
void MyClockDemo::paintEvent(QPaintEvent *)
{
    qDebug()<<"paintEvent";
    //下面三个数组用来定义表针的三个顶点，以便后面的填充
        static const QPoint hourHand[3] = {
            QPoint(3, 8),
            QPoint(-3, 8),
            QPoint(0, -40)
        };  //时针
        static const QPoint minuteHand[3] = {
            QPoint(3, 8),
            QPoint(-3, 8),
            QPoint(0, -70)
        };  //分针
        static const QPoint secondHand[3] = {
            QPoint(3, 8),
            QPoint(-3, 8),
            QPoint(0, -90)
        };//秒针

        QColor hourColor(127,0,127);
        QColor minColor(0,127,127,127);
        QColor secColor(127,127,0,127);

        QPainter painter(this);        

        painter.setRenderHint(QPainter::Antialiasing);
        painter.translate(width()/2,height()/2);
        int w=qMin(width(),height());
        painter.scale(w/300.0,w/300.0);
        painter.save();

        painter.setPen(QColor(255,0,0));
        QString timeStr=QTime::currentTime().toString();
        painter.drawText(-40,30,80,30,Qt::AlignCenter|Qt::AlignTop,timeStr);

        QTime time=QTime::currentTime();

        painter.restore();
        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(secColor);
        painter.rotate(time.second()*6);
        painter.drawConvexPolygon(secondHand,3);

        painter.restore();
        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(minColor);
        painter.rotate((time.minute()+time.second()/60.0)*6.0);
        painter.drawConvexPolygon(minuteHand,3);

        painter.restore();
        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(hourColor);
        painter.rotate((time.hour()+time.minute()/60.0)*30.0);
        painter.drawConvexPolygon(hourHand,3);

        painter.restore();
        painter.setBrush(Qt::black);
        painter.drawEllipse(QPoint(0,0),2,2);

        painter.setBrush(Qt::NoBrush);
        painter.setPen(Qt::black);
        painter.drawEllipse(QPoint(0,0),97,97);

}

